package com.base.cn.platform.os.controller.user.exprule;

import com.base.cn.platform.os.common.AdminBaseController;
import com.base.cn.platform.os.common.utils.DataUtil;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.service.manage.user.exprule.CusUserExpRuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * 后台经验规则
 *
 * @author ll
 * @create 2018-04-18-19:08
 */
@Controller
@RequestMapping("/admin/user/cus")
public class AdminCusUserExpRuleController extends AdminBaseController {

    @Autowired
    private CusUserExpRuleService cusUserExpRuleService;
    /**
     * 去列表页面
     * @return
     */
    @RequestMapping("/userExpruleList")
    public String  queryExpruleListToPage(){
        return resultPage("/user/exprule/exprule_list");
    }

    /**
     * 搜索经验列表
     * @return
     */
    @RequestMapping("/ajaxUserExpruleList")
    public String ajaxUserExpruleList(Model model,
                                      @RequestParam(value = "currentPage",required = false,defaultValue = "1")int currentPage,
                                      @RequestBody Map<String,Object> params){
        Map<String,Object> dataMap = cusUserExpRuleService.queryCusUserExpRulePage(params,currentPage);
        model.addAttribute("dataMap",dataMap);
        return resultPage("/user/exprule/exprule_ajax_list");
    }

    /**
     * 修改经验规则
     * @param id
     * @param score
     * @return
     */
    @RequestMapping("/updateUserExprule")
    @ResponseBody
    public Map<String,Object> updateUserExprule(@RequestParam("id")BigDecimal id,
                                                @RequestParam("score")Integer score){
        if(!DataUtil.idIsNotNull(id)){
            return ResultUtil.ERROR("经验规则id 不能为空");
        }
        Map<String,Object> params = new HashMap<>();
        params.put("id",id);
        params.put("score",score);
        Map<String,Object> expruleMap =cusUserExpRuleService.saveUpdateCusUserExpRule(params);
        if(ObjectUtils.isNotEmpty(expruleMap)){
            return ResultUtil.SUCCESS("修改成功");
        }
        return ResultUtil.ERROR("系统繁忙请稍后再试");
    }

    /**
     * 修改经验规则
     * @param params 添加数据
     * @return
     */
    @RequestMapping("/saveUserExprule")
    @ResponseBody
    public Map<String,Object> saveUserExprule(@RequestBody Map<String,Object> params){
        if(!ObjectUtils.isNotEmpty(params)){
            return ResultUtil.ERROR("添加数据不能为空");
        }
        Map<String,Object> expruleMap =cusUserExpRuleService.saveUpdateCusUserExpRule(params);
        if(ObjectUtils.isNotEmpty(expruleMap)){
            return ResultUtil.SUCCESS("添加成功");
        }
        return ResultUtil.ERROR("系统繁忙请稍后再试");
    }

    /**
     * 修改经验状态
     * @param id
     * @param status
     * @return
     */
    @RequestMapping("/updateCusUserExpruleStatus")
    @ResponseBody
    public Map<String,Object> updateCusUserExpruleStatus(@RequestParam("id")BigDecimal id,
                                                     @RequestParam("status")Integer status){
        if (!DataUtil.idIsNotNull(id)||status<=0){
            return ResultUtil.ERROR("修改数据不能为空");
        }
        Map<String,Object> expruleMap =cusUserExpRuleService.updateCusUserExpRuleStatus(id,status);
        if(ObjectUtils.isNotEmpty(expruleMap)){
            return ResultUtil.SUCCESS("修改成功");
        }
        return ResultUtil.ERROR("系统繁忙请稍后再试");
    }

    /**
     * 去经验添加页面
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/toSaveUserEcprulePage")
    public String toSaveUserEcprulePage(Model model,
                                        @RequestParam(value = "id",required = false)BigDecimal id){
        Map<String,Object> userPruleMap =new HashMap<>();
        if(DataUtil.idIsNotNull(id)){
            userPruleMap =cusUserExpRuleService.queryCusUserExpRuleFindById(id,new ArrayList<>());
        }
        model.addAttribute("userPruleMap",userPruleMap);
        return resultPage("/user/exprule/exprule_create");
    }
}
